package vip.xiaonuo.common.util;

import com.alibaba.fastjson2.JSON;
import vip.xiaonuo.common.wechat.enums.WechatBotKeyEnum;
import vip.xiaonuo.common.wechat.enums.WechatNotificationType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjt
 * @description
 * @date 2025/5/21 16:01
 */
public class CommonWechatUtil {

    /**
     * 企微通知：文本
     */
    public static void sendMessage(List<String> userList, String content, String key) {
        Map<String, Object> body = new HashMap<>();
        body.put("msgtype", "text");
        Map<String, Object> textContent = new HashMap<>();
        textContent.put("content", content);
        textContent.put("mentioned_mobile_list", userList);
        body.put("text", textContent);
        CommonHttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + key, null, JSON.toJSONString(body), "企业微信通知");
    }

    /**
     * 企微通知：markdown
     */
    public static void sendMessage(String title, String content, WechatNotificationType type, List<String> noticeUserList) {
        Map<String, Object> body = new HashMap<>();
        body.put("msgtype", "markdown");
        StringBuilder markdownContent = new StringBuilder("#".repeat(type.getTitleLevel()) + " " + title + "\n\n\n\n")
                .append("<font color=\"").append(type.getColor()).append("\">").append(content).append("</font>\n\n\n\n");
        for (String user : noticeUserList) {
            markdownContent.append("<@").append(user).append("> ");
        }
        body.put("markdown", Map.of("content", markdownContent.toString()));
        CommonHttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=67b11f96-6cfa-431c-a3f9-74b95923a1b5", null, JSON.toJSONString(body), "企业微信通知");
    }

    /**
     * 企微通知：markdown
     * 后期只留这一个
     */
    public static void sendMessage(String title, WechatNotificationType type, List<String> noticeUserList, String... content) {
        Map<String, Object> body = new HashMap<>();
        body.put("msgtype", "markdown");
        StringBuilder markdownContent = new StringBuilder("#".repeat(type.getTitleLevel()) + " " + title + "\n\n\n\n");
        for (int i = 0; i < content.length; i++) {
            markdownContent.append("<font color=\"").append(type.getColor()).append("\">").append(content[i]).append("</font>\n\n\n\n");
        }
        for (String user : noticeUserList) {
            markdownContent.append("<@").append(user).append("> ");
        }
        body.put("markdown", Map.of("content", markdownContent.toString()));
        CommonHttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=67b11f96-6cfa-431c-a3f9-74b95923a1b5", null, JSON.toJSONString(body), "企业微信通知");
    }

    /**
     * 企微通知：markdown
     * 后期只留这一个
     */
    public static void sendMessage(WechatBotKeyEnum wechatBotKeyEnum, WechatNotificationType type, String title, List<String> noticeUserList, String... content) {
        Map<String, Object> body = new HashMap<>();
        body.put("msgtype", "markdown");
        StringBuilder markdownContent = new StringBuilder("#".repeat(type.getTitleLevel()) + " " + title + "\n\n\n\n");
        for (int i = 0; i < content.length; i++) {
            markdownContent.append("<font color=\"").append(type.getColor()).append("\">").append(content[i]).append("</font>\n\n\n\n");
        }
        for (String user : noticeUserList) {
            markdownContent.append("<@").append(user).append("> ");
        }
        body.put("markdown", Map.of("content", markdownContent.toString()));
        CommonHttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + wechatBotKeyEnum.getKey(), null, JSON.toJSONString(body), "企业微信通知");
    }
}
